package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import orm.ChildrenBehaviorPurpose;
import orm.CollectivityMonthDomain;
import orm.CollectivityWeekArrange;
import orm.CollectivityWeekTimeBucket;
import util.DBHelper;

public class CMonthDomainDAO {
	
	public void insertCMonthDomain(Statement statement, 
			ArrayList<CollectivityMonthDomain> cmdArr, String cMonthArrangeID) throws SQLException {
		CollectivityMonthDomain cmd = null;
		for (int i = 0; i < cmdArr.size(); i++) {
			cmd = cmdArr.get(i);
			statement.execute("insert into collectivitymonthdomain values('" + cMonthArrangeID + "'," +
					cmd.getDomainID() + ", '" + cmd.getRemark() + "')");
		}
	}
	
	public void updateCMonthDomain(Statement statement,
			ArrayList<CollectivityMonthDomain> cmdArr, String cMonthArrangeID) throws SQLException {
		CollectivityMonthDomain cmd = null;
		for (int i = 0; i < cmdArr.size(); i++) {
			cmd = cmdArr.get(i);

			StringBuffer sqlStr = new StringBuffer();
			sqlStr.append("update collectivitymonthdomain set Remark = '" + cmd.getRemark() + "'");
			sqlStr.append("where CMonthArrangeID = '" + cmd.getCMonthArrangeID() + "' and DomainID = " + cmd.getDomainID() + "");
			statement.execute(sqlStr.toString());
		}
	}
	
	public void deleteCMonthDomain(Statement statement, String cMonthArrangeID) throws SQLException {
		statement.execute("delete from collectivitymonthdomain where CMonthArrangeID='" + cMonthArrangeID + "'");
	}
	
	public ArrayList<CollectivityMonthDomain> getCMDByCMAID(String cMonthArrangeID) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		ArrayList<CollectivityMonthDomain> al = new ArrayList<CollectivityMonthDomain>();
		CollectivityMonthDomain cmd = null;
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery("select * from collectivitymonthdomain where CMonthArrangeID='" + cMonthArrangeID + "'");
		
		while (rs.next()) {
			cmd = new CollectivityMonthDomain();
			int i = 1;
			cmd.setCMonthArrangeID(rs.getString(i++));
			cmd.setDomainID(rs.getInt(i++));
			cmd.setRemark(rs.getString(i++));
			al.add(cmd);
		}
		rs.close();
		stmt.close();
		conn.close();
		
		return al;
	}
}
